public class Test9 {
    public static void main(String[] args) {
        int[] grid1 = {0,0,1,0,0,0,0,1,0,0,0,0,0};
        int[] grid2 = {0,0,0,0,0,0,0,1,1,1,0,0,0};
        int[] grid3 = {0,1,1,0,1,0,0,0,0,0,0,0,0};
        int[] grid4 = {0,1,0,0,1,1,0,0,1,0,1,0,0};
        int[] grid5 = {0,1,0,0,1,1,0,0,1,1,1,0,0};
        int[] grid6 = {0,0,0,0,0,0,0,0,0,0,1,0,0};
        int[] grid7 = {0,0,0,0,0,0,0,1,1,1,0,0,0};
        int[] grid8 = {0,0,0,0,0,0,0,1,1,0,0,0,0};
        int[][] grid = {grid1, grid2, grid3, grid4, grid5, grid6, grid7, grid8};
        int a = maxAreaOfIsland(grid);
        System.out.println(a);
    }

    static int m, n;
    static boolean[][] vis;
    static int[] dx = {0, 0, 1, -1};
    static int[] dy = {1, -1, 0, 0};
    static int max = 0;
    static int area = 0;
    public static int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        vis = new boolean[m][n];
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == 1 && vis[i][j] == false) {
                    area = 1;
                    vis[i][j] = true;
                    dfs(grid, i, j);
                }
            }
        }
        return max;
    }

    public static void dfs(int[][] grid, int i, int j) {
        max = Math.max(max, area);

        for(int k = 0; k < 4; k++) {
            int x = dx[k] + i;
            int y = dy[k] + j;

            if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && vis[x][y] == false) {
                vis[x][y] = true;
                area++;
                dfs(grid, x, y);
            }
        }
    }
}
